﻿@model IContentModel
@{
    ViewBag.Title = "Select your move";
}
<h2>
    @ViewBag.Title</h2>
<div id="proggy_moveselector">
    <p class="italic">
        Use the content tree below to chose where "@(Model.Name)" should be moved to, or
        @Html.ActionLink("Cancel", "Index", new { success = 5, selected = Model.Id })</p>
    @{
        // get id for site map
        var areaId = "sm_" + DateTime.Now.ToFileTimeUtc() + "_sm";

        // render site map
        Html.RenderAction("SiteMap", new { areaId = areaId, controller = "Content", area = "Proggy", includeContentMenu = false, asPublished = false });

                    // add script function
        <script type="text/javascript">

            Proggy.Static.WaitForTypesToRegister(function () {
            
            // 
            function openCloseCallback(){
                
                // find selected
                var selected = jQuery('a[data-nodeid = "@Model.Id"]').parent("li");//.proggy-sm-selectedTrue:first");
                
                // find open/close click, unbind and re-bind
                var openClose = selected.find("a.proggy-sm-link-closed:first");

                // replace
                openClose.replaceWith("<span id=\"noopenclose_@(areaId)\" class=\"proggy-sm-link proggy-sm-link-closed\">&nbsp;</span>");
                
                // get new click
                jQuery("#noopenclose_@(areaId)").click(function(e){
                    e.preventDefault();
                    alert("Cannot open here - this is the parent content you are moving");
                });
                
                
                // find text link
                var link = selected.find(".proggy-sm-preview-link:first");
                
                // change color
                link.css({
                    color:"red"
                });
            };

            // set up url
            var url = '@Url.Action("Move")';

            //
            var siteMap = new Proggy.SiteMap();

            

            //
            siteMap.Init("@areaId", false, false, function (node) {
     
                // get id
                var nodeId = node.data("nodeid");

                // check and bind alert
                if(nodeId== @Model.Id){
                    
                    alert("Content cannot move to itself");

                }
                else {

                    // get final url
                    var finalUrl = url + "/" + nodeId + "?nodeToMoveId=@Model.Id";

                    // add method
                    Proggy.Static.AddToBeforeLightboxContentShowMethods(function () {

                        // find lightbox
                        var lb = Proggy.Static.GetLightbox(0);
                        
                        // get content
                        var content = lb.find("#proggy_contenthasmoved");
                        
                        // check
                        if (content.size() > 0) {
                            
                            // show loader then re-direct
                            Proggy.Static.ShowTempLoader(lb, Proggy.Static.GetLightboxInner(lb),function(){
                                window.location = content.attr("href");
                            });
                        }

                    });

                    //window.location = finalUrl;
                    Proggy.Static.InitialLoadIntoLightbox(jQuery("<a href=\""+finalUrl+"\"></a>"), function(lb, lbInner){
                        
                        

                    });
                }  
                           

            }, openCloseCallback);

            // run function initially
            openCloseCallback();

        });

        </script>
    }
</div>
